package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Dao.AreaRepository;
import com.realtyManage.springboot.Dao.SysLogRepository;
import com.realtyManage.springboot.Model.Do.Area;
import com.realtyManage.springboot.Model.Do.SysLog;
import com.realtyManage.springboot.Model.Vo.BaseEntity;
import com.realtyManage.springboot.Model.Vo.SysLogEntity;
import com.realtyManage.springboot.Service.AreaService;
import com.realtyManage.springboot.Service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaRepository areaRepository;

    @Override
    public Area save(Area area) {
        return areaRepository.saveAndFlush(area);
    }

    @Override
    public Page<Area> findPagedByWhereClause(BaseEntity entity) {
        //分页查询
        Integer pageNumber = entity.getPageNumber()-1;
        Integer pageSize = entity.getPageSize();

        //排序条件
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(pageNumber,pageSize,sort);

        //动态查询条件
        Specification<Area> specification = (Specification<Area>)(root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        return areaRepository.findAll(specification, pageable);
    }

    @Override
    public int deleteArea(String areaId) {
        return areaRepository.deleteArea(areaId);
    }

}
